<?php

namespace app\common\model;

class Attachment extends BaseModel
{
    protected $table = 'attachment';
    public function getAttDirAttr($value)
    {
        return addImagePrefix($value);
    }
    public function getSattDirAttr($value)
    {
        return addImagePrefix($value);
    }
    /**添加附件记录
     */
    public static function attachmentAdd($name,$att_size,$att_type,$att_dir,$satt_dir='',$pid = 0, $mer_id = null )
    {
        $data['mer_id'] = $mer_id;
        $data['name'] = $name;
        $data['att_dir'] = $att_dir;
        $data['satt_dir'] = $satt_dir;
        $data['att_size'] = intval($att_size);
        $data['att_type'] = $att_type;
        $data['time'] = now();
        $data['pid'] = $pid;
        return self::create($data);
    }
    /**
     * 获取分类图
     * */
    public static function getAll($id){
        $model = new self;
        $where['pid'] = $id;
        return $model = $model->where($where)->order('att_id desc')->paginate(24);
//        return self::pager($model, [], null, 24);
    }
    /**
     * 获取单条信息
     * */
    public static function getinfo($att_id){
        $model = new self;
        $where['att_id'] = $att_id;
        return $model->where($where)->select()->toArray()[0];
    }

    public static function deleteItem($where)
    {
        $attinfo = self::where($where)->find();
        if($attinfo == null) exception('未查询到记录');
        @unlink(env('root_path'). 'public' .  removeImagePrefix($attinfo['att_dir']));
        @unlink(env('root_path'). 'public' .  removeImagePrefix($attinfo['satt_dir']));
        return self::where($where)->delete();
    }
}